package com.sugar.subscribe.course.module.business.service.venue;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sugar.subscribe.course.module.business.dal.dataobject.city.CityDO;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachvenue.CoachVenueDO;
import com.sugar.subscribe.course.module.business.dal.mysql.city.CityMapper;
import com.sugar.subscribe.course.module.business.dal.mysql.coachvenue.CoachVenueMapper;
import com.sugar.subscribe.course.module.business.service.city.CityService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.sugar.subscribe.course.module.business.controller.admin.venue.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.venue.VenueDO;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;

import com.sugar.subscribe.course.module.business.dal.mysql.venue.VenueMapper;

import static com.sugar.subscribe.course.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.subscribe.course.framework.common.util.collection.CollectionUtils.diffList;
import static com.sugar.subscribe.course.module.business.enums.ErrorCodeConstants.*;

/**
 * 场地 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class VenueServiceImpl implements VenueService {

    @Resource
    private VenueMapper venueMapper;
    @Resource
    private CityService cityService;

    @Resource
    private CoachVenueMapper coachVenueMapper;

    @Override
    public Long createVenue(VenueSaveReqVO createReqVO) {
        // 获取城市信息
        CityDO city = cityService.getCityValidate(createReqVO.getCityId());

        // 插入
        VenueDO venue = BeanUtils.toBean(createReqVO, VenueDO.class);
        venue.setCityName(city.getName());
        venueMapper.insert(venue);

        // 返回
        return venue.getId();
    }

    @Override
    public void updateVenue(VenueSaveReqVO updateReqVO) {
        // 校验存在
        validateVenueExists(updateReqVO.getId());
        // 获取城市信息
        CityDO city = cityService.getCityValidate(updateReqVO.getCityId());
        // 更新
        VenueDO updateObj = BeanUtils.toBean(updateReqVO, VenueDO.class);
        updateObj.setCityName(city.getName());
        venueMapper.updateById(updateObj);

        // 更新教练场地表冗余的名称
        coachVenueMapper.update(Wrappers.<CoachVenueDO>lambdaUpdate()
                .eq(CoachVenueDO::getVenueId, updateObj.getId())
                .set(CoachVenueDO::getVenueName, updateObj.getName())
                .set(CoachVenueDO::getCityId, updateObj.getCityId())
                .set(CoachVenueDO::getCityName, updateObj.getCityName()));
    }

    @Override
    public void deleteVenue(Long id) {
        // 校验存在
        validateVenueExists(id);
        // 删除
        venueMapper.deleteById(id);
    }

    @Override
        public void deleteVenueListByIds(List<Long> ids) {
        // 删除
        venueMapper.deleteByIds(ids);
        }


    private VenueDO validateVenueExists(Long id) {
        VenueDO venueDO = venueMapper.selectById(id);
        if (venueDO == null) {
            throw exception(VENUE_NOT_EXISTS);
        }
        return venueDO;
    }

    @Override
    public VenueDO getVenue(Long id) {
        return validateVenueExists(id);
    }

    @Override
    public PageResult<VenueDO> getVenuePage(VenuePageReqVO pageReqVO) {
        return venueMapper.selectPage(pageReqVO);
    }

    @Override
    public List<VenueSimpleRespVO> getVenueAll() {
        return BeanUtils.toBean(venueMapper.selectList(), VenueSimpleRespVO.class);
    }

    @Override
    public List<VenueSimpleRespVO> getVenuesByCityId(Long cityId) {
        return BeanUtils.toBean(venueMapper.selectList(VenueDO::getCityId, cityId), VenueSimpleRespVO.class);
    }

}